package com.gexiaobao.pigeon.ui.fragment.mall.evaluate

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.ViewCommentsResponse
import com.gexiaobao.pigeon.databinding.FragmentViewCommentsBinding
import com.gexiaobao.pigeon.viewmodel.EvaluateViewModel
import com.google.android.material.tabs.TabLayout
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/28 14:51
 * @Describe    : 商品评价
 */
class FragmentViewComments : BaseFragment<EvaluateViewModel, FragmentViewCommentsBinding>() {

    private var totalEvalCount = 0
    private var goodId = 0

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = getString(R.string.comment_on_commodity)
        goodId = requireArguments().getInt("goodId")
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        onRefresh()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            nav().navigateUp()
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.viewCommentsResult.observe(viewLifecycleOwner) {
            setTabUI(it)
        }
    }

    private fun setTabUI(it: ViewCommentsResponse) {
        totalEvalCount = it.total
        val orderIndex = 0
        val tab1 = mDatabind.tabLayout.newTab().setText(getString(R.string.activity_mine_account_transaction_type1_all) + "($totalEvalCount)")
        mDatabind.tabLayout.addTab(tab1)
        val tab2 = mDatabind.tabLayout.newTab().setText(getString(R.string.shop_good_reputation_tab) + "(" + it.goodEavlTotal + ")")
        mDatabind.tabLayout.addTab(tab2)
        val tab3 = mDatabind.tabLayout.newTab().setText(getString(R.string.medium_review) + "(" + it.MiddleEavlTotal + ")")
        mDatabind.tabLayout.addTab(tab3)
        val tab4 = mDatabind.tabLayout.newTab().setText(getString(R.string.negative_comment)+ "(" + it.BadEavlTotal + ")")
        mDatabind.tabLayout.addTab(tab4)

        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {

            }

            override fun onTabUnselected(p0: TabLayout.Tab) {

            }

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        replaceFragments(orderIndex)
        mDatabind.tabLayout.getTabAt(orderIndex)!!.select()
    }

    private fun replaceFragments(position: Int) {
        childFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.evaluate_container, FragmentViewCommentsList.newInstance(0, goodId))
                1 -> replace(R.id.evaluate_container, FragmentViewCommentsList.newInstance(1, goodId))
                2 -> replace(R.id.evaluate_container, FragmentViewCommentsList.newInstance(2, goodId))
                3 -> replace(R.id.evaluate_container, FragmentViewCommentsList.newInstance(3, goodId))
            }
        }.commitAllowingStateLoss()
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getReviewsList(true, goodId, 0)
    }

}